
<style>
    .spanColor{
        color: red;
    }
    .layui-form-label {
        width: 150px;
    }
    .layui-input{
        width: 200px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
<!--                        <label class="layui-form-label w-auto">名称：</label>-->
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="请输入类型名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" type="button" lay-submit="" lay-filter="formSubSearchLog">搜索</button>
                        <button id="storeProductBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <!--<button id="customerContactPostBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="dicTable" lay-filter="dicTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="dicTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">设置</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="storeTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add" style="background-color: #006E33;color: white">添加到智能柜存放物资类型</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="dicForm">
    <form lay-filter="dicForm" class="layui-form model-form">
      <!--  <div class="layui-form-item">
            <input name="id" type="hidden"/>
            <label class="layui-form-label">编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入编码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字典类型</label>
            <div class="layui-input-block">
                <select name="dicTypeId" id="dicTypeId"></select>
            </div>
        </div>-->
        <!--搜索-->
        <div class="layui-input-inline" style="margin-left: 10px">
            <input name="name" class="layui-input" type="text" placeholder="请输入类型名称"/>
        </div>
        <button class="layui-btn icon-btn" type="button" lay-submit="" lay-filter="addFormSubSearchLog">搜索</button>
        <!-- 表格 -->
        <table class="layui-table" id="addTable" lay-filter="addTable"></table>

<!--        <div class="layui-form-item text-right">-->
<!--            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>-->
<!--            <button class="layui-btn" lay-filter="dicFormSubmit" lay-submit>保存</button>-->
<!--        </div>-->
    </form>
</script>

<!--编辑表单弹窗-->
<script type="text/html" id="upDatadicForm">
    <form lay-filter="upDatadicForm" class="layui-form model-form" id="upForm">

        <div class="layui-form-item">
            <label class="layui-form-label"><span class="spanColor">*</span>类型名</label>
            <div class="layui-input-block">
                <input name="name"  type="text" class="layui-input"  style="border: none" readonly/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span class="spanColor">*</span>类型编号</label>
            <div class="layui-input-block">
                <input name="code"  type="text" class="layui-input" style="border: none" readonly/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input name="remark"  type="text" class="layui-input" style="border: none" readonly/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">库存预警最小计量单位</label>
            <div class="layui-input-inline">
                <select id="unitId" name="unitId" lay-filter="unitId" >
                    <option value=""> 请选择单位</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">库存预警阈值</label>
            <div class="layui-input-block">
                <input name="thresholdValue" placeholder="请输入库存预警阈值" min="0"  type="number" class="layui-input" lay-verify="required" required style="width: 200px;" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">归还时长阈值</label>
            <div class="layui-input-block">
                <input name="thresholdBlack" placeholder="单位为小时(h)" min="0" type="number" class="layui-input" lay-verify="required" required style=" width: 200px;"/>
            </div>
        </div>

        <input name="id" type="hidden">
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="upDataFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;


        // 渲染表格
        var insTb = table.render({
            elem: '#dicTable',
            url: config.base + 'enterprisecab/cabProduct/admin/storeProductType/getCabCuttertypeList?type=is',
            page: true,
            cellMinWidth: 100,
            cols: [[
                // {type: 'checkbox'}
                 {type: 'numbers', title: '序号'}
                ,{field: 'name', title: '类型名称'}
                ,{field: 'code', title: '类型编号'}
                ,{field: 'thresholdValue',  title: '库存预警阈值'}
                ,{field: 'thresholdBlack',  title: '未还预警阈值'}
                ,{field: 'createTime',  title: '添加时间'}
                ,{field: 'creatorName',  title: '添加人'}
                ,{field: 'updateTime',  title: '参数编辑时间'}
                ,{field: 'modifierName',  title: '最新参数编辑人'}
                ,{align: 'center', toolbar: '#dicTableBar', title: '操作', width: 200}
            ]],
          /*  done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }*/
        });

        // 工具条点击事件
        table.on('tool(dicTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showUpdataModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 工具条点击事件
        table.on('tool(addTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'add') { //添加智能柜存放物资类型
                layer.confirm('确定要添加到智能柜存放物资类型？', {title: "操作提示", btn: ['确定', '取消']}
                , function(){ // 确定操作
                        addSearch(data);
                });
            }
        });

        //监听排序
        table.on('sort(dicTable)', function(obj){
            table.reload('dicTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#storeProductBtnAdd').click(function () {
            showEditModel();
        });

        function addSearch(data){
            admin.req('enterprisecab/cabProduct/admin/storeProductType/upProductType',data, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    form.render('select');
                    table.reload('addTable');
                    table.reload('dicTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'PUT');
        }


        // 显示添加弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: ['55%', '80%'],
                offset: '120px',
                title: data ? '修改智能柜存放物资类型' : '添加智能柜存放物资类型',
                content: $('#dicForm').html(),
                success: function () {
                    if (!data){
                        // 渲染表格
                        var insTbs = table.render({
                            elem: '#addTable',
                            url: config.base + 'enterprisecab/cabProduct/admin/storeProductType/getCabCuttertypeList?type=no',
                            page: true,
                            cellMinWidth: 100,
                            cols: [[
                                {type: 'numbers', title: '序号'}
                                ,{field: 'name', sort: true, title: '类型名称'}
                                ,{field: 'code', sort: true, title: '类型编号'}
                                ,{field: 'createTime', sort: true, title: '添加时间'}
                                ,{align: 'center', toolbar: '#storeTableBar', title: '操作', width: 200}
                            ]],
                        });
                    }
                    // 搜索
                    form.on('submit(addFormSubSearchLog)', function (data) {
                        insTbs.reload({where: data.field, page: {curr: 1}}, 'data');
                    });
                    form.val('dicForm', data);
                    // 表单提交事件
    /*                form.on('submit(dicFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/system/admin/dic/update' : 'enterprise/system/admin/dic/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('dicTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');

                        return false;
                    });*/
                }
            });
        }
        // 修改弹窗
        function showUpdataModel(data) {
            admin.open({
                type: 1,
                area: ['55%', '80%'],
                offset: '120px',
                title:  '修改智能柜存放物资类型',
                content: $('#upDatadicForm').html(),
                success: function () {

                    // 库存预警最小计量单位
                    admin.req('enterprise/product/admin/productUnit/list',{}, function (res) {
                        if (res.code == 200) {
                            loadSelectHtml(res.data, "请选择","unitId", "name");
                            var select = 'dd[lay-value=' + data.unitId + ']';
                            var changeselect = $('#unitId').siblings("div.layui-form-select").find('dl').find(select).click();
                            form.render("select");
                        }
                    }, 'GET');
                    // 下拉框回显
                    if (data.thresholdValue===0){
                        data.thresholdValue=""
                    }
                    if (data.thresholdBlack===0){
                        data.thresholdBlack=""
                    }
                    form.val('upDatadicForm', data);


                    // 表单提交事件
                    form.on('submit(upDataFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req('enterprisecab/cabProduct/admin/storeProductType/upDate', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('dicTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },  'PUT');

                        return false;
                    });
                }
            });

        }

        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         */
        function loadSelectHtml(data, tips, htmltarget, name) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                html += `<option value='${data[i].id}'>${data[i][name]}</option>`;
            }
            $("[name='" + htmltarget + "']").html(html);
            form.render('select');
        }


        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/dic/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }
        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 导出excel
        $('#dicBtnExport').click(function () {
            var checkRows = table.checkStatus('dicTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>